<?php

require FCPATH . '/application/controllers/backend/backend.php';

class Slide_show extends Backend {

    public function __construct() {
	parent::__construct();
	$this->load->model('slide_show_model', 'model');
    }

    public function index() {
	$this->load->config('global');

	$data = array(
	    'types' => $this->config->item('slide_show_types'),
	    'l_position' => array(
		array('name' => '轮播图片')
	    )
	);

	$this->set_js('SLIDE_SHOW_TYPES', json_encode($data['types']));

	$this->load_js(array('jquery-ui-1.8.20.custom.min', 'ImageBrowser', 'backend_slide_show'));
	$this->load_css(array('custom-theme/jquery-ui-1.8.20.custom'));

	$this->rend('slide_show', $data);
    }

    public function records() {
	$page = intval($this->input->get('page'));
	$page_size = intval($this->input->get('pageSize'));
	$page = $page ? $page : 1;
	$page_size = $page_size ? $page_size : 10;
	$offset = $page_size * ($page - 1);

	$conditions = array();
	$id = intval($this->input->get('id'));
	if (!empty($id)) {
	    $conditions[] = array('where', 'id', $id);
	}

	$type = intval($this->input->get('type'));
	if (!empty($type)) {
	    $conditions[] = array('where', 'type', $type);
	    if ($type > 1) {
		$parent = intval($this->input->get('parent'));
		if (!empty($parent)) {
		    $conditions[] = array('where', 'parent', $parent);
		}
	    }
	}

	$start = $this->input->get('start');
	if (!empty($start)) {
	    $conditions[] = array('where', 'created_at >=', $start);
	}

	$end = $this->input->get('end');
	if (!empty($end)) {
	    $conditions[] = array('where', 'created_at <=', $end);
	}

	$order_fields = array('image', 'type', 'parent', 'created_at');
	$order_field = intval($this->input->get('order_field'));
	$order_type = intval($this->input->get('order_type'));
	$orders = array();
	if ($order_field >= 0 && $order_field < count($order_fields)) {
	    $orders = array(
		array($order_fields[$order_field], $order_type == 1 ? 'asc' : 'desc')
	    );
	}
	$this->json_back('XT001', '', array(
	    'total' => $this->model->total($conditions),
	    'records' => $this->model->records(array(), $conditions, $orders, $page_size, $offset)
	));
    }

    public function add() {
	$this->load->config('global');

	$data = array(
	    'types' => $this->config->item('slide_show_types'),
	    'l_position' => array(
		array('name' => '轮播图片', 'path' => '/backend/slide_show/index'),
		array('name' => '添加图片')
	    )
	);

	$this->load_js(array('jquery-ui-1.8.20.custom.min', 'ImageBrowser', 'backend_slide_show_form'));
	$this->load_css(array('custom-theme/jquery-ui-1.8.20.custom', 'backend_slide_show_form'));
	$this->rend('slide_show_form', $data);
    }

    public function edit() {
	$id = intval($this->uri->segment(4));
	$this->load->config('global');
	$data = array(
	    'types' => $this->config->item('slide_show_types'),
	    'slide_show' => $this->model->find($id),
	    'l_position' => array(
		array('name' => '轮播图片', 'path' => '/backend/slide_show/index'),
		array('name' => '修改图片')
	    )
	);
	$this->set_js('PARENT',$data['slide_show']->parent);
	$this->load_js(array('jquery-ui-1.8.20.custom.min', 'ImageBrowser', 'backend_slide_show_form'));
	$this->load_css(array('custom-theme/jquery-ui-1.8.20.custom', 'backend_slide_show_form'));

	$this->rend('slide_show_form', $data);
    }

    public function insert() {
	$data = $this->getPostData();

	$this->validate($data);

	if (($id = $this->model->insert($data)) > 0) {
	    $this->json_back('XT001', '添加成功');
	}
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }

    public function update() {
	$id = intval($this->input->post('id'));
	$data = $this->getPostData();

	$this->validate($data);

	if ($this->model->update($id, $data)) {
	    $this->json_back('XT001', '更新成功');
	}
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }
    
    private function getPostData(){
	return array(
	    'type' => intval($this->input->post('type')),
	    'parent' => intval($this->input->post('parent')),
	    'image' => intval($this->input->post('image')),
	    'link' => intval($this->input->post('link')),
	    'description_zh' => $this->input->post('description_zh'),
	    'description_fr' => $this->input->post('description_fr'),
	    'description_en' => $this->input->post('description_en'),
	);
    }

    public function delete() {
	$id = intval($this->input->get('id'));
	if ($this->model->delete($id)) {
	    $this->json_back('XT001', '删除成功。');
	}
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }

    private function validate($data) {
	if (empty($data['type'])) {
	    $this->json_back('XT002', '请选择关联类型');
	}

	if ($data['type'] > 1 && empty($data['parent'])) {
	    $this->json_back('XT002', '请选择关联内容');
	}

	if (empty($data['image'])) {
	    $this->json_back('XT002', '请选择图片');
	}

	if (empty($data['description_zh'])) {
	    $this->json_back('XT002', '请填写描述（中文）');
	}

	if (empty($data['description_fr'])) {
	    $this->json_back('XT002', '请填写描述（法文）');
	}

	if (empty($data['description_en'])) {
	    $this->json_back('XT002', '请填写描述（英文）');
	}
    }

}
